package com.jinyi.wwdeng.ui.main.fragment

import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import androidx.core.content.ContextCompat
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.FragmentCreditRepaymentResultBinding
import com.jinyi.wwdeng.pro.ProFragment
import com.jinyi.wwdeng.ui.user.act.UserFeedbackAct
import com.jinyi.wwdeng.utils.event.TrackEventType
import com.jinyi.wwdeng.utils.event.TrackEventUtils
import java.util.Random

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/24 16:58
 * @Annotation：贷款还款结果（还款成功、还款失败）
 */
class CreditRepaymentResultFragment: ProFragment() {

    lateinit var binding: FragmentCreditRepaymentResultBinding
    override fun getRootView(inflater: LayoutInflater): View {
        binding = FragmentCreditRepaymentResultBinding.inflate(inflater)
        return binding.root
    }

    override fun initView() {
        setTitleBarVisibility(View.VISIBLE)
        setBackBtnVisibility(View.INVISIBLE)
        setTextTitleVisibility(View.INVISIBLE)
        setRightBtnRes(R.mipmap.pic_kefu_black_icon)
        setTitleBarColor(ContextCompat.getColor(requireContext(), com.jinyi.lib_base.R.color.color_ffffff))

        initRefreshLayout()
    }

    override fun initListener() {
        //客服
        rootViewBinding.ivFun.setOnClickListener {
            onIntent(UserFeedbackAct::class.java)
            TrackEventUtils.logEvent(TrackEventType.CLICK.CLICK_CUSTOMER,"CreditRepaymentResult Page")
        }

    }

    override fun initData() {
        loadDataAction()
    }

    /* 初始化刷新 */
    private fun initRefreshLayout() {
        //禁止加载
        binding.refreshLayout.setEnableLoadMore(false)
        binding.refreshLayout.setEnableAutoLoadMore(false)
        binding.refreshLayout.setOnRefreshListener {
            loadDataAction()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            binding.refreshLayout.finishLoadMore()
        }
    }

    /**
     * 还款成功
     */
    private fun showSuccessView(){
        binding.tipsLayout.setBackgroundResource(R.drawable.base_radius_frame_b2f0de_10)
        binding.imgIcon.setImageResource(R.mipmap.pic_credit_success_icon)
        binding.tvTipsTitle.setTextColor(ContextCompat.getColor(requireContext(),R.color.color_00ce90))
        binding.tvTipsTitle.text = getString(R.string.awesome)
        binding.tvTipsMsg.text = getString(R.string.your_loan_has_been_settled_keep_applying_your_loan_limit_will_continue_to_increase)
        binding.ivReturn.visibility = View.VISIBLE
        binding.nextLayout.visibility = View.VISIBLE
        binding.btnRepayNow.visibility = View.GONE
    }

    /**
     * 还款失败
     */
    private fun showFailedView(){
        binding.tipsLayout.setBackgroundResource(R.drawable.base_radius_frame_f7afaf_10)
        binding.imgIcon.setImageResource(R.mipmap.pic_credit_apply_failed_icon)
        binding.tvTipsTitle.setTextColor(ContextCompat.getColor(requireContext(),R.color.color_ff4d3e))
        binding.tvTipsTitle.text = getString(R.string.payment_failed)
        binding.tvTipsMsg.text = getString(R.string.reason_of_failure_exceeds_withdrawal_limit)
        binding.ivReturn.visibility = View.GONE
        binding.nextLayout.visibility = View.GONE
        binding.btnRepayNow.visibility = View.VISIBLE
    }

    /**
     * 加载数据
     */
    private fun loadDataAction(){
        val isSuccess = Random().nextBoolean()
        if (isSuccess){
            showSuccessView()
        } else {
            showFailedView()
        }

        Handler().postDelayed({
            binding.refreshLayout.finishRefresh()
        },1000)
    }
}